﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using WFNetLib.PacketProc;
using WFNetLib;

namespace 点滴架调试
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        CP1616Packet ComRxPacket;
        private void Form2_Load(object sender, EventArgs e)
        {
            //this.WindowState = FormWindowState.Maximized;
            CheckForIllegalCrossThreadCalls = false;
            string[] ports = SerialPort.GetPortNames();
            Array.Sort(ports);
            cbCom.Items.Clear();
            cbCom.Items.AddRange(ports);
            cbCom.SelectedIndex = 0;            
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            if (Com.IsOpen)
                Com.Close();
            Com.PortName = cbCom.Text;
            Com.Open();
            ComRxPacket = new CP1616Packet(0x03, 0x0001);
            byte[] txbuffer = CP1616Packet.MakeCP1616Packet(0x03, 0x0001, 0x0);
            Com.Write(txbuffer, 0, txbuffer.Length);
            toolStripButton1.Enabled = false;
            toolStripButton2.Enabled = false;
            timer1.Enabled = true;
        }
        byte[] data;
        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            if (Com.IsOpen)
                Com.Close();
            Com.PortName = cbCom.Text;
            Com.Open();
            data=new byte[26];
            data[0] = (byte)(numericUpDown4.Value-1);
            data[1] = BytesOP.GetLowByte((UInt16)numericUpDown1.Value);
            data[2] = BytesOP.GetHighByte((UInt16)numericUpDown1.Value);
            data[3] = (byte)numericUpDown2.Value;
            data[4] = (byte)numericUpDown2.Value;

            data[5] = BytesOP.GetLowByte(UInt16.Parse(textBox31.Text));
            data[6] = BytesOP.GetHighByte(UInt16.Parse(textBox31.Text));

            data[7] = BytesOP.GetLowByte(UInt16.Parse(textBox25.Text));
            data[8] = BytesOP.GetHighByte(UInt16.Parse(textBox25.Text));

            data[9] = BytesOP.GetLowByte(UInt16.Parse(textBox21.Text));
            data[10] = BytesOP.GetHighByte(UInt16.Parse(textBox21.Text));

            UInt16 x = UInt16.Parse(textBox22.Text);
            x = (UInt16)(x * 10);
            data[11] = BytesOP.GetLowByte(x);
            data[12] = BytesOP.GetHighByte(x);

            data[13] = BytesOP.GetLowByte(UInt16.Parse(textBox23.Text));
            data[14] = BytesOP.GetHighByte(UInt16.Parse(textBox23.Text));

            x = UInt16.Parse(textBox24.Text);
            x = (UInt16)(x * 10);
            data[15] = BytesOP.GetLowByte(x);
            data[16] = BytesOP.GetHighByte(x);

            x = UInt16.Parse(textBox26.Text);
            x = (UInt16)(x * 10);
            data[17] = BytesOP.GetLowByte(x);
            data[18] = BytesOP.GetHighByte(x);

            data[19] = BytesOP.GetLowByte(UInt16.Parse(textBox27.Text));
            data[20] = BytesOP.GetHighByte(UInt16.Parse(textBox27.Text));

            x = UInt16.Parse(textBox28.Text);
            x = (UInt16)(x * 10);
            data[21] = BytesOP.GetLowByte(x);
            data[22] = BytesOP.GetHighByte(x);

            data[23] = byte.Parse(textBox29.Text);

            data[24] = BytesOP.GetLowByte(UInt16.Parse(textBox30.Text));
            data[25] = BytesOP.GetHighByte(UInt16.Parse(textBox30.Text));

            ComRxPacket = new CP1616Packet(0x02, 0x0001);
            byte[] txbuffer = CP1616Packet.MakeCP1616Packet(0x02, 0x0001, data);
            Com.Write(txbuffer, 0, txbuffer.Length);
            toolStripButton1.Enabled = false;
            toolStripButton2.Enabled = false;
            timer1.Enabled = true;
        }
        private void Com_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            int rxn = Com.BytesToRead;
            while (rxn != 0)
            {
                rxn--;
                byte rx = (byte)Com.ReadByte();
                if (ComRxPacket.DataPacketed(rx))
                {
                    if (ComRxPacket.NeedCommand == 0x02)
                    {
                        timer1.Enabled = false;
                        toolStripButton1.Enabled = true;
                        toolStripButton2.Enabled = true;
                        if ((ComRxPacket.Data[0] == 'O') && ComRxPacket.Data[1] == 'K')
                        {
                            MessageBox.Show("设置参数成功!!!!!");
                        }
                        else
                        {
                            MessageBox.Show("设置参数失败!!!!!");
                        }
                    }
                    else if (ComRxPacket.NeedCommand == 0x03)
                    {
                        timer1.Enabled = false;
                        toolStripButton1.Enabled = true;
                        toolStripButton2.Enabled = true;
                        UInt16 weight = ComRxPacket.Data[0];
                        numericUpDown4.Value = weight + 1;//频段需要+1
                        weight = BytesOP.MakeShort(ComRxPacket.Data[2], ComRxPacket.Data[1]);
                        numericUpDown1.Value = weight;//护士编号
                        weight = ComRxPacket.Data[3];
                        numericUpDown2.Value = weight;//房间编号
                        weight = ComRxPacket.Data[4];
                        numericUpDown3.Value = weight;//架子编号

                        weight = BytesOP.MakeShort(ComRxPacket.Data[6], ComRxPacket.Data[5]);
                        textBox31.Text = weight.ToString();
                        weight = BytesOP.MakeShort(ComRxPacket.Data[8], ComRxPacket.Data[7]);
                        textBox25.Text = weight.ToString();
                        weight = BytesOP.MakeShort(ComRxPacket.Data[10], ComRxPacket.Data[9]);
                        textBox21.Text = weight.ToString();
                        weight = BytesOP.MakeShort(ComRxPacket.Data[12], ComRxPacket.Data[11]);
                        weight = (UInt16)(weight / 10);
                        textBox22.Text = weight.ToString();
                        weight = BytesOP.MakeShort(ComRxPacket.Data[14], ComRxPacket.Data[13]);
                        textBox23.Text = weight.ToString();
                        weight = BytesOP.MakeShort(ComRxPacket.Data[16], ComRxPacket.Data[15]);
                        weight = (UInt16)(weight / 10);
                        textBox24.Text = weight.ToString();
                        weight = BytesOP.MakeShort(ComRxPacket.Data[18], ComRxPacket.Data[17]);
                        weight = (UInt16)(weight / 10);
                        textBox26.Text = weight.ToString();
                        weight = BytesOP.MakeShort(ComRxPacket.Data[20], ComRxPacket.Data[19]);
                        textBox27.Text = weight.ToString();
                        weight = BytesOP.MakeShort(ComRxPacket.Data[22], ComRxPacket.Data[21]);
                        weight = (UInt16)(weight / 10);
                        textBox28.Text = weight.ToString();
                        weight = ComRxPacket.Data[23];
                        textBox29.Text = weight.ToString();
                        weight = BytesOP.MakeShort(ComRxPacket.Data[25], ComRxPacket.Data[24]);
                        textBox30.Text = weight.ToString();                        
                    }
                    else
                    {
                        
                    }
                }
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {
                if (ComRxPacket.NeedCommand == 0x03)
                {
                    byte[] txbuffer = CP1616Packet.MakeCP1616Packet(0x03, 0x0001, 0x0);
                    Com.Write(txbuffer, 0, txbuffer.Length);
                }
                else if (ComRxPacket.NeedCommand == 0x02)
                {
                    byte[] txbuffer = CP1616Packet.MakeCP1616Packet(0x02, 0x0001, data);
                    Com.Write(txbuffer, 0, txbuffer.Length);
                }
                else
                    timer1.Enabled = false;
            }
            catch
            {
            	
            }
            
        }

        private void Form2_FormClosing(object sender, FormClosingEventArgs e)
        {
            Com.Close();
        }

       
    }
}
